package com.wei.controller;


import com.wei.entity.GdWaterfee;
import com.wei.mapper.GdWaterfeeMapper;
import com.wei.mapper.GdWaterfeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李泽伟
 * @since 2022-03-08
 */
@RestController
@RequestMapping
public class GdWaterfeeController {

    @Autowired
    private GdWaterfeeMapper waterfeeMapper;

    @RequestMapping("/getWaterfees")
    public List<GdWaterfee> getWaterfee(){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        return waterfeeMapper.selectByMap(map);
    }

    @RequestMapping("/addWaterfee")
    public void addWaterfee(@RequestParam Map<String,Object> waterfeeObj){
        GdWaterfee waterfee =setParam(waterfeeObj);
        waterfeeMapper.insert(waterfee);
    }

    @RequestMapping("/updateWaterfee")
    public void updateWaterfee(@RequestParam Map<String,Object> waterfeeObj){
        GdWaterfee waterfee = setParam(waterfeeObj);
        waterfeeMapper.updateById(waterfee);
    }

    @RequestMapping("/deleteWaterfee")
    public void deleteWaterfee(@RequestParam String waterfeeId){
        waterfeeMapper.deleteById(waterfeeId);
    }

    /**
     * 通过房产地址、月份获取水费记录，用于月度账单的保存生成(管理员)
     * @param paramObj
     * @return
     */
    @RequestMapping("/getWaterfee")
    public Double getWaterfee(@RequestParam Map<String,Object> paramObj){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("waterfee_date", paramObj.get("feeDate"));
        map.put("property_address", paramObj.get("propertyAddress"));
        return waterfeeMapper.selectByMap(map).get(0).getWaterfeeTotalfee();
    }

    /**
     * 通过房产地址获取水费记录，用于水费的查询(普通用户)
     * @param address
     * @return
     */
    @RequestMapping("/getWaterfeeByAddress")
    public List<GdWaterfee> getWaterfeeByAddress(@RequestParam String address){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("property_address", address);
        return waterfeeMapper.selectByMap(map);
    }


    /**
     * 设置参数方法
     * @param waterfeeObj
     * @return
     */
    private GdWaterfee setParam(@RequestParam Map<String, Object> waterfeeObj) {
        GdWaterfee waterfee=new GdWaterfee();
        waterfee.setWaterfeeId((String) waterfeeObj.get("waterfeeId"));
        waterfee.setWaterfeeLreading(Integer.parseInt(waterfeeObj.get("waterfeeLreading").toString()));
        waterfee.setWaterfeeTreading(Integer.parseInt(waterfeeObj.get("waterfeeTreading").toString()));
        waterfee.setWaterfeeAmount(Integer.parseInt(waterfeeObj.get("waterfeeAmount").toString()));
        waterfee.setWaterfeePrice(Double.parseDouble(waterfeeObj.get("waterfeePrice").toString()));
        waterfee.setWaterfeeTotalfee(Double.parseDouble(waterfeeObj.get("waterfeeTotalfee").toString()));
        waterfee.setWaterfeeDate((String) waterfeeObj.get("waterfeeDate"));
        waterfee.setWaterfeeForeignkey((String) waterfeeObj.get("waterfeeForeignkey"));
        waterfee.setPropertyAddress((String) waterfeeObj.get("propertyAddress"));
        return waterfee;
    }

}

